#!/usr/bin/env python

##  flamingo - 2D Game Engine
##  Copyright (C) 2009 Bradley Zeis
##
##  This program is free software: you can redistribute it and/or modify
##  it under the terms of the GNU General Public License as published by
##  the Free Software Foundation, either version 3 of the License, or
##  (at your option) any later version.
##
##  This program is distributed in the hope that it will be useful,
##  but WITHOUT ANY WARRANTY; without even the implied warranty of
##  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
##  GNU General Public License for more details.
##
##  You should have received a copy of the GNU General Public License
##  along with this program.  If not, see <http://www.gnu.org/licenses/>.
##
##  Bradley Zeis
##  bradzeis@gmail.com

import os
import unittest
import pickle
import flamingo

class TestLoad(unittest.TestCase):

    def testLoad(self):
        good = [(os.path.join(flamingo.fl_path, "tests/res/flamingo.png"), -1, []),
                (os.path.join(flamingo.fl_path, "tests/res/glass.bmp"), -1, []),
                (os.path.join(flamingo.fl_path, "tests/res/test.gif"), -1, [(), ()]),
                (os.path.join(flamingo.fl_path, "tests/res/test.gif"), 740, [])]

        for g in good:
            self.assertTrue(isinstance(flamingo.image.load(*g), flamingo.image.Texture))

    def testBad(self):
        b = [(os.path.join(flamingo.fl_path, "tests/res/flamingo.png"), "-1", []),
             (os.path.join(flamingo.fl_path, "tests/res/flaming.png"), -1, []),
             (os.path.join(flamingo.fl_path, "tests/res/flamingo.png"), "-1", [(1,)])]

        self.assertRaises(TypeError, flamingo.image.load, *b[0])
        self.assertRaises(flamingo.image.LoadingError, flamingo.image.load, *b[1])
        self.assertRaises(TypeError, flamingo.image.load, *b[2])

class TestLoadSheet(unittest.TestCase):

    def testLoadSheetH(self):
        g = [(os.path.join(flamingo.fl_path, "tests/res/swatch.png"), 100, 0, []),
             (os.path.join(flamingo.fl_path, "tests/res/swatch.png"), 600, 0, []),
             (os.path.join(flamingo.fl_path, "tests/res/swatch.png"), 100, 0, [(), ()]),
             (os.path.join(flamingo.fl_path, "tests/res/swatch.png"), 0, 100, []),
             (os.path.join(flamingo.fl_path, "tests/res/swatch.png"), 700, 100, [])]

        t = flamingo.image.load_sheeth(*g[0])
        self.assertTrue(isinstance(t, flamingo.image.Texture))
        self.assertEqual(t.width, 100)
        self.assertEqual(t.height, 100)
        self.assertEqual(len(t), 6)

        t = flamingo.image.load_sheeth(*g[1])
        self.assertTrue(isinstance(t, flamingo.image.Texture))
        self.assertEqual(t.width, 600)
        self.assertEqual(t.height, 100)
        self.assertEqual(len(t), 1)

        t = flamingo.image.load_sheeth(*g[2])
        self.assertTrue(isinstance(t, flamingo.image.Texture))
        self.assertEqual(t.width, 100)
        self.assertEqual(t.height, 100)
        self.assertEqual(len(t), 6)

        t = flamingo.image.load_sheeth(*g[3])
        self.assertTrue(isinstance(t, flamingo.image.Texture))
        self.assertEqual(t.width, 1)
        self.assertEqual(t.height, 100)
        self.assertEqual(len(t), 600)

        t = flamingo.image.load_sheeth(*g[4])
        self.assertTrue(isinstance(t, flamingo.image.Texture))
        self.assertEqual(t.width, 600)
        self.assertEqual(t.height, 100)
        self.assertEqual(len(t), 1)

    def testLoadSheetV(self):
        g = [(os.path.join(flamingo.fl_path, "tests/res/swatchv.png"), 100, 0, []),
             (os.path.join(flamingo.fl_path, "tests/res/swatchv.png"), 600, 0, []),
             (os.path.join(flamingo.fl_path, "tests/res/swatchv.png"), 100, 0, [(), ()]),
             (os.path.join(flamingo.fl_path, "tests/res/swatchv.png"), 0, 100, []),
             (os.path.join(flamingo.fl_path, "tests/res/swatchv.png"), 700, 100, [])]

        t = flamingo.image.load_sheetv(*g[0])
        self.assertTrue(isinstance(t, flamingo.image.Texture))
        self.assertEqual(t.width, 100)
        self.assertEqual(t.height, 100)
        self.assertEqual(len(t), 6)

        t = flamingo.image.load_sheetv(*g[1])
        self.assertTrue(isinstance(t, flamingo.image.Texture))
        self.assertEqual(t.width, 100)
        self.assertEqual(t.height, 600)
        self.assertEqual(len(t), 1)

        t = flamingo.image.load_sheetv(*g[2])
        self.assertTrue(isinstance(t, flamingo.image.Texture))
        self.assertEqual(t.width, 100)
        self.assertEqual(t.height, 100)
        self.assertEqual(len(t), 6)

        t = flamingo.image.load_sheetv(*g[3])
        self.assertTrue(isinstance(t, flamingo.image.Texture))
        self.assertEqual(t.width, 100)
        self.assertEqual(t.height, 1)
        self.assertEqual(len(t), 600)

        t = flamingo.image.load_sheetv(*g[4])
        self.assertTrue(isinstance(t, flamingo.image.Texture))
        self.assertEqual(t.width, 100)
        self.assertEqual(t.height, 600)
        self.assertEqual(len(t), 1)

    def testBad(self):
        b = [(os.path.join(flamingo.fl_path, "tests/res/swatchv.png"), "100", 1, []),
             (os.path.join(flamingo.fl_path, "tests/res/swatchvt.png"), 100, 750, []),
             (os.path.join(flamingo.fl_path, "tests/res/swatchv.png"), 100, "750", []),
             (os.path.join(flamingo.fl_path, "tests/res/swatchv.png"), 100, 750, [(1,)])]

        self.assertRaises(TypeError, flamingo.image.load_sheeth, *b[0])
        self.assertRaises(flamingo.image.LoadingError, flamingo.image.load_sheeth, *b[1])
        self.assertRaises(TypeError, flamingo.image.load_sheeth, *b[2])
        self.assertRaises(TypeError, flamingo.image.load_sheeth, *b[3])

        self.assertRaises(TypeError, flamingo.image.load_sheetv, *b[0])
        self.assertRaises(flamingo.image.LoadingError, flamingo.image.load_sheetv, *b[1])
        self.assertRaises(TypeError, flamingo.image.load_sheetv, *b[2])
        self.assertRaises(TypeError, flamingo.image.load_sheetv, *b[3])

class TestTexture(unittest.TestCase):
    def setUp(self):

        self.t = flamingo.image.load(os.path.join(flamingo.fl_path, "tests/res/test.gif"))
        self.width = 347
        self.height = 326
        self.speed = 750
        self.length = 5

    def testAccess(self):
        self.assertEqual(self.t.width, self.width)
        self.assertEqual(self.t.height, self.height)
        self.assertEqual(self.t.speed, self.speed)
        self.assertEqual(len(self.t), self.length)
        self.assertEqual(self.t.frame, 0)

        self.t.speed = -2
        self.t.frame = 6

        self.assertEqual(self.t.speed, -2)
        self.assertEqual(self.t.frame, 1)

        try:
            self.t.width = 400
            self.fail()
        except TypeError:
            pass

        try:
            self.t.height = 400
            self.fail()
        except TypeError:
            pass


    def testUpdate(self):
        self.t.speed = 750
        self.t.frame = 0

        self.t.update(700)
        self.assertEqual(self.t.frame, 0)
        self.t.update(50)
        self.assertEqual(self.t.frame, 1)

        self.t.frame = 0
        self.t.update(3750)
        self.assertEqual(self.t.frame, 0)


##-------- Test Suite
testload      = unittest.TestLoader().loadTestsFromTestCase(TestLoad)
testloadsheet = unittest.TestLoader().loadTestsFromTestCase(TestLoadSheet)
testtexture   = unittest.TestLoader().loadTestsFromTestCase(TestTexture)

testsuite = unittest.TestSuite((testload, testloadsheet, testtexture))

if __name__ == "__main__":
    unittest.TextTestRunner(verbosity=2).run(testsuite)
